<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>
        <a href>商品分类</a>
      </el-breadcrumb-item>
      <el-breadcrumb-item>优惠券列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card v-loading="loading">
      <!-- 行     gutter是格式这里的间隔-->
      <el-row :gutter="20">
        <!-- 列 -->
        <el-col :span="8">
          <!-- 搜索框 -->
          <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getCoupon">
            <el-button slot="append" icon="el-icon-search" @click="getCoupon"></el-button>
          </el-input>
        </el-col>
        <!-- 列 -->
        <el-col :span="4">
          <el-button type="primary" @click="goAddPage">添加优惠券</el-button>
        </el-col>
      </el-row>

      <!-- 用户列表区域 -->
      <el-table :data="couponList" border stripe>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column label="优惠券名称" prop="name" width="150px"></el-table-column>
        <el-table-column label="优惠券面值" prop="faceValue" width="100px"></el-table-column>
        <el-table-column label="满减金额" prop="how_much" width="90px"></el-table-column>
        <el-table-column label="提示" prop="usingTips" width="90px"></el-table-column>
        <el-table-column label="有效期时间" prop="termOfValidity" width="90px">
          <template slot-scope="scope">
            {{scope.row.termOfValidity | dateFormat}}
          </template>
        </el-table-column>
        <el-table-column label="使用范围" prop="useScope" width="90px"></el-table-column>
        <el-table-column label="位置" prop="position" width="90px"></el-table-column>

        <el-table-column label="状态" width="90px">
          <!-- 作用域插槽 -->
          <template slot-scope="scope">
            <el-switch v-model="scope.row.upShelves" @change="upperShelfStateChange(scope.row)"></el-switch>
          </template>
        </el-table-column>

        <el-table-column label="操作" width="130px">
          <!-- 作用域插槽 -->
          <!-- slot-scope="scope" -->
          <template slot-scope="scope">
            <!-- 修改按钮 -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="showEditDialog(scope.row._id)"
            ></el-button>
            <!-- 删除按钮 -->
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="removeCouponById(scope.row._id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pageNum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
  </div>
</template>
<script>
import { getCouponList, delCouponByid, updateCouponUpShelvesState } from '../../api/coupon/list.js'
export default {
  data() {
    return {
      loading: false,
      queryInfo: {
        query: null,
        // 当前的页数
        pageNum: 1,
        // 当前每页显示多少条
        pageSize: 5
      },
      total: 0,
      couponList: []
    }
  },
  created() {
    this.getCoupon()
  },
  methods: {
    getCoupon() {
      console.log('取优惠的列表')
      getCouponList(this.queryInfo).then(res => {
        console.log(res)
        if (!res.data.success) {
          return this.$message.error('获取优惠券失败')
        }
        this.couponList = res.data.data
        console.log(this.couponList)
      })
    },
    goAddPage() {
      this.$router.push('/coupon/add')
    },
    showEditDialog(id) {
      this.$router.push('/coupon/update?id=' + id)
    },
    removeCouponById(id) {
      console.log(id)
      this.$confirm('此操作将永久删除该优惠券, 是否继续?', '删除优惠券提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(res => {
        console.log(res)
        if (res !== 'confirm') return
        this.loading = true
        delCouponByid(id).then(res => {
          console.log(res)
          this.loading = false
          if (res.data.deleted === 0) return this.$message.error('删除优惠券失败')
          // 重新获优惠券列表
          this.getCoupon()
          this.$message.success('删除指定优惠券完成')
        })
      })
      this.loading = false
    },
    upperShelfStateChange(item) {
      console.log(item)
      const params = {
        id: item._id,
        UpperShelfState: item.upShelves
      }
      console.log(params)
      updateCouponUpShelvesState(params).then(res => {
        // console.log(res)
        if (res.data.data.modified > 0) {
          this.$message.success('商品上架状态更新成功')
        }
      })
    },
    handleSizeChange() {},
    handleCurrentChange() {}
  }
}
</script>
<style lang="less" scoped>
.bg-purple {
  background: #d3dce6;
}
.bg-purple-light {
  background: #e5e9f2;
}
.grid-content {
  border-radius: 4px;
  min-height: 36px;
}
</style>
